using UnityEngine;
using System.Collections;

public class SlotMachine : MonoBehaviour 
{
	public const int SLOTNUMBER = 3;
	
	public GameObject prefabs;
	Slot [] slot = new Slot[SLOTNUMBER];
	
	public const int START = 0;
	public const int BEGINEND = 1;
	public const int BEGINEND2 = 2;
	public const int END = 3;

	PlayScene scene;
	
	public int state;
		
	void Start () 
	{
		state = END;
	}
	
	public void Create(float x, float y, float magin)
	{
		string [] str = { "atk", "def", "etc" };
		for(int i=0; i<slot.Length; ++i)
		{
			slot[i] = new Slot();
			slot[i].prefab = prefabs;
			slot[i].Create(new Vector3(x + magin*i, y, -10), new Vector2(32,32));
		}
	}
	
	public void SetScene(PlayScene s)
	{
		scene = s;
	}
	
	public void AddCard(int nIndex, float x, float y, int ID, Texture view)
	{
		if( nIndex >= slot.Length )
			return;
		slot[nIndex].AddCard(x, y, ID, view);
	}
	
	public void SlotStart()
	{
		if( state == END )
		{
			state = START;
			for(int i=0; i<slot.Length; ++i)
			{
				slot[i].Start ();
			}
		}
	}
	
	public void SlotStop(int nIndex)
	{
		if( state != START && nIndex >= slot.Length )
			return ;
		
		state = BEGINEND2;
		slot[nIndex].Stop();
	}
	
	public void SlotStop()
	{
		if( state != START && state != BEGINEND )
			return;
		
		state = BEGINEND;
		slot[0].Stop();
	}
	
	void Update () 
	{
		if( slot[0] == null )
			return;
		if( state == END )
			return;
		int i;
		for(i=0; i<slot.Length; ++i)
		{
			slot[i].Update();
			
			if( slot[i].State == Slot.SlotState.Stop )
			{
				slot[(i+1)%slot.Length].Stop();
			}
		}
		//*
		for(i=0; i<slot.Length; ++i)
		{
			if( slot[i].State != Slot.SlotState.Stop )
			{
				break;
			}
		}
		if( i == slot.Length )
		{
			scene.OnSlotEvent(slot, this);
			state = END;
		}
		//*/
	}
}
